<%@ page import="com.googlecode.jwsm.*" %><%@ page import="java.lang.reflect.*" %><%@ page import="java.io.*" %><%
	response.setContentType("text/plain");

	Service service = ServiceManager.getInstance().getService(request.getParameter("service"));
	String className = Character.toUpperCase(service.getName().charAt(0)) + service.getName().substring(1) + "ServiceClient";
	Method[] methods = service.getMethods(); 
	String jwsmURL = request.getRequestURL().toString();
	jwsmURL = jwsmURL.substring(0, jwsmURL.lastIndexOf('/'));
	String url = jwsmURL + "/ServiceRequest/" + service.getName() + "/";
	
	response.addHeader("Content-Disposition", "attachment; filename=\"" + className + ".java\"");
%>import org.jcommon.xml.*;

import java.io.*;
import java.net.*;
import java.rmi.*;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

/**
 * <%= className %> was generated dynamically by Java Web Service Manager.
 * The dependencies for this class are as follows:
 *		- XMLSerialization (<a href="<%= jwsmURL %>/download/xmlserialization.jar">Download</a>)
 */
public class <%= className %> {
<%
	for (int i = 0; i < methods.length; i++) {
		String returnString = methods[i].getReturnType().getName();
		if (returnString.startsWith("java.io")) {
			returnString = returnString.substring("java.io.".length());
		} else if (returnString.startsWith("java.lang")) {
			returnString = returnString.substring("java.lang.".length());
		} else if (returnString.startsWith("java.net")) {
			returnString = returnString.substring("java.net.".length());
		}
		if (methods[i].getReturnType().isArray()) {
			returnString = returnString.substring(2, returnString.length() - 1) + "[]";
		}
		Class[] classes = methods[i].getParameterTypes();
		StringBuffer params = new StringBuffer();
		for (int j = 0; j < classes.length; j++) {
			if (j > 0) params.append(", ");
			String s = classes[j].getName();
			if (s.startsWith("java.io")) {
				s = s.substring("java.io.".length());
			} else if (s.startsWith("java.lang")) {
				s = s.substring("java.lang.".length());
			} else if (s.startsWith("java.net")) {
				s = s.substring("java.net.".length());
			}
			if (classes[j].isArray()) {
				s = s.substring(2, s.length() - 1) + "[]";
			}
			params.append(s + " arg" + j);
		}
%>
	public <%= returnString %> <%= methods[i].getName() %>(<%= params.toString() %>) throws Exception {
<% if (classes.length > 0) { %>		// Generate SOAP Message
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		buffer.append("<SOAP-ENV:Header/>");
		buffer.append("<SOAP-ENV:Body>");
<%
	for (int j = 0; j < classes.length; j++) {
		String argument = "arg" + j;
		if (classes[j].isPrimitive()) {
			if (classes[j] == boolean.class) {
				argument = "new Boolean(arg" + j + ")";
			} else if (classes[j] == byte.class) {
				argument = "new Byte(arg" + j + ")";
			} else if (classes[j] == char.class) {
				argument = "new Character(arg" + j + ")";
			} else if (classes[j] == short.class) {
				argument = "new Short(arg" + j + ")";
			} else if (classes[j] == int.class) {
				argument = "new Integer(arg" + j + ")";
			} else if (classes[j] == long.class) {
				argument = "new Long(arg" + j + ")";
			} else if (classes[j] == float.class) {
				argument = "new Float(arg" + j + ")";
			} else if (classes[j] == double.class) {
				argument = "new Byte(arg" + j + ")";
			}
		}
		if (classes[j] == String.class) {
%>
		buffer.append(arg<%= j %>);
<%
		} else {
%>
		buffer.append(XMLSerialization.toXMLString(<%= argument %>, null, false).replaceAll("<\\?.*\\?>", "").trim());
<%
		}
	}
%>
		buffer.append("</SOAP-ENV:Body>");
		buffer.append("</SOAP-ENV:Envelope>");
<% } %>
		// Make request
		URL url = new URL("<%= url + methods[i].getName() %>");
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		connection.setDoOutput(true);
<%
	if (classes.length > 0) {
		if (classes[0] == InputStream.class) {
%>
		byte[] b = new byte[512];
		int len;
		OutputStream out = connection.getOutputStream();
		while ((len = arg0.read(b)) != -1) {
			out.write(b, 0, len);
		}
		out.flush();
		out.close();
		arg0.close();
<%
		} else {
%>
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
		writer.write(buffer.toString());
		writer.flush();
<%
		}
	}
	if (methods[i].getReturnType() == InputStream.class) {
%>
		return connection.getInputStream();
<%
	} else {
%>
		// Read response
		if (connection.getResponseCode() != 200) throw new IOException("Bad response code: " + connection.getResponseMessage());
		Document document = new SAXBuilder().build(new BufferedReader(new InputStreamReader(connection.getInputStream())));
		String prefix = document.getRootElement().getNamespacePrefix();
		Element fault = (Element)XPath.selectSingleNode(document, "//" + prefix + ":Envelope/" + prefix + ":Body/" + prefix + ":Fault/*");
		if (fault != null) {
			Throwable t = (Throwable)XMLSerialization.fromXML(fault, Thread.currentThread().getContextClassLoader());
			throw new RemoteException("The remote machine threw an exception", t);
		}
<% 		if (methods[i].getReturnType() == String.class) { %>
		Element body = (Element)XPath.selectSingleNode(document, "//" + prefix + ":Envelope/" + prefix + ":Body");
		return body.getTextTrim();
<% 		} else { %>
		Element content = (Element)XPath.selectSingleNode(document, "//" + prefix + ":Envelope/" + prefix + ":Body/*");
<%
			if (methods[i].getReturnType().isPrimitive()) {
				Class returnType = methods[i].getReturnType();
				if (returnType == boolean.class) {
					%>		return ((Boolean)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).booleanValue();<%
				} else if (returnType == byte.class) {
					%>		return ((Byte)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).byteValue();<%
				} else if (returnType == char.class) {
					%>		return ((Character)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).charValue();<%
				} else if (returnType == short.class) {
					%>		return ((Short)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).shortValue();<%
				} else if (returnType == int.class) {
					%>		return ((Integer)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).intValue();<%
				} else if (returnType == long.class) {
					%>		return ((Long)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).longValue();<%
				} else if (returnType == float.class) {
					%>		return ((Float)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).floatValue();<%
				} else if (returnType == double.class) {
					%>		return ((Double)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader())).doubleValue();<%
				}
			} else {
%>
				return (<%= returnString %>)XMLSerialization.fromXML(content, Thread.currentThread().getContextClassLoader());
<%
			}
		}
	}
%>
	}
<%
	}
%>
}